﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using DragDropTest;

namespace FileUploader {
    public partial class UploaderControl : IView {
        private readonly Uploader uploader;
       
        public UploaderControl() {
            
            InitializeComponent();
            
            
            uploader = new Uploader(this);
                uploader.setServerFiles();
            ButtonDelete.IsEnabled = false;
            listBoxServerFiles.SelectionChanged += ListBoxServerFilesSelectionChanged;
            ButtonUpload.Click += ButtonUploadClick;
            Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e) {
            AnimateImage.Begin();
        }

        [ScriptableMember]
        public void Start(){
            Visibility = Visibility.Visible;
            
        }

        void ListBoxServerFilesSelectionChanged(object sender, SelectionChangedEventArgs e) {
            if(e.AddedItems != null && e.AddedItems.Count>0){
                ButtonDelete.IsEnabled = true;
                return;
            }
            ButtonDelete.IsEnabled = false;
        }


        private void ButtonUploadClick(object sender, RoutedEventArgs e) {
            var dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "Images (*.png;*.gif;*.jpg)|*.png;*.gif;*.jpg | All files (*.*)|*.*";
            StatusText.Text = "";
            bool? retval = dlg.ShowDialog();

            if (retval != null && retval == true) // open/uploaded
            {
                string fileName = dlg.File.Name;
                StatusText.Text = "Uploading " + fileName;
                UploadFile(dlg.File);

            }
            else //cancel
            {
                StatusText.Text = "No file selected...";
            }
        }

        private void UploadFile(FileInfo file) {
            uploader.UploadFile(file);
        }

        private void listBoxServerFiles_Drop(object sender, DragEventArgs e) {
            FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
            var first = files[0];
            uploader.UploadFile(first);
        }

        public void SetStatusText(string status){
            StatusText.Text = status;
        }

        public ProgressBar ProgressBar1{
            get { return progressBar1; }
        }

        public DragableListBox DragableListBox{
            get { return listBoxServerFiles; }
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e) {
            if(listBoxServerFiles.SelectedItems.Count >0){
                string message = "Are you sure you want to delete:\r\n\r\n";
                foreach (DemoData item in listBoxServerFiles.SelectedItems){
                    message += item.Name + Environment.NewLine;
                }
                if (MessageBox.Show(message, "Delete Files", MessageBoxButton.OKCancel) == MessageBoxResult.OK){
                    var files = from item in listBoxServerFiles.SelectedItems.Cast<DemoData>()
                                select  item.Name.ToString();
                    uploader.deleteFiles(files.ToList());
                }
                ButtonDelete.IsEnabled = false;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            Visibility = Visibility.Collapsed;
            
        }

        private void ButtonDelete_Click_1(object sender, RoutedEventArgs e) {

        }

        
    }
}
